package cn.nuist.service.impl;

import cn.nuist.dao.SupplierDao;
import cn.nuist.entity.Supplier;
import cn.nuist.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class SupplierServiceImpl implements SupplierService {

    private SupplierDao supplierDao;

    @Autowired
    public SupplierServiceImpl(SupplierDao supplierDao) {
        this.supplierDao = supplierDao;
    }

    @Override
    public List<Supplier> getAllSupplier() {
        return supplierDao.getAllSupplier();
    }

    @Override
    public void add(Supplier supplier) {
        Supplier supplierByPhone = getSupplierByPhone(supplier.getContactPhone());
        //System.out.println("***"+supplierByPhone);
        if(supplierByPhone!=null){
            throw new RuntimeException("已有该供应商！！！");
        }
        supplierDao.add(supplier);
    }

    @Override
    public Supplier getSupplierByPhone(String contactPhone) {
        return supplierDao.getSupplierByPhone(contactPhone);
    }

    @Override
    public Supplier getSupplierByName(String supplierName) {
        Supplier supplierByName = supplierDao.getSupplierByName(supplierName);
        if(Objects.isNull(supplierByName)){
            throw new RuntimeException("该供应商不存在！！！");
        }
        return supplierByName;
    }

    @Override
    public void delete(int supplierID) {
        supplierDao.delete(supplierID);
    }

    @Override
    public Supplier getSupplierById(int supplierID) {
        return supplierDao.getSupplierById(supplierID);
    }

    @Override
    public void update(Supplier supplier) {
        supplierDao.update(supplier);
    }
}
